package com.gome.boot.config.dubbo.filter;

import com.alibaba.dubbo.rpc.*;
import org.apache.commons.lang.time.DateFormatUtils;
import org.slf4j.Logger;

import java.util.Date;

/**
 * Dubbo调用日志输出过滤器.
 *
 * @author baoxiufeng
 */
public class InvokeLogFilter implements Filter {
    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(InvokeLogFilter.class);

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        RpcContext context = RpcContext.getContext();
        String application = context.getUrl().getParameter("application");
        String datetime = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
        if (context.isProviderSide()) {
            logger.info("Provider application {} by invoked from consumer {} at {}.", application, context.getRemoteAddressString(), datetime);
        } else {
            logger.info("Consumer application {} invoke provider {} at {}.", application, context.getRemoteAddressString(), datetime);
        }
        return invoker.invoke(invocation);
    }
}
